import { createRouter, createWebHistory } from "vue-router";
import Index from "./components/Index.vue";
import HomePage from "./components/HomePage.vue";
import Login from "./components/Login.vue";
const routes = [
  {
    path: "/",
    redirect: "/login",
    meta: { role: 200 },
  },
  {
    path: "/login",
    component: Login,
    meta: { role: 200 },
  },
  {
    path: "/index",
    component: Index,
    children: [
      {
        path: "/home",
        component: () => import("./components/HomePage.vue"),
        meta: { title: "首页", role: 2 },
      },
      {
        path: "/record/personal",
        component: () => import("./components/record/PersonRecord.vue"),
        meta: { title: "个人档案", role: 2 },
      },
      {
        path: "/record/personal/EsFamily",
        component: () => import("./components/record/EstablishingFamily.vue"),
        meta: { title: "组建家庭", role: 2 },
      },
      {
        path: "/record/personal/details",
        component: () => import("./components/record/PerRecordDetails.vue"),
        meta: { title: "个人基本信息表", role: 2 },
      },
      {
        path: "/record/family",
        component: () => import("./components/record/FamilyRecord.vue"),
        meta: { title: "家庭档案", role: 2 },
      },
      {
        path: "/record/family/details",
        component: () => import("./components/record/FamRecordDetails.vue"),
        meta: { title: "家庭档案信息表", role: 2 },
      },
      {
        path: "/user/umsg",
        component: () => import("./components/user/UserMessage.vue"),
        meta: { title: "个人中心", role: 2 },
      },
      {
        path: "/user/upassword",
        component: () => import("./components/user/UserPassword.vue"),
        meta: { title: "修改密码", role: 2 },
      },
      {
        path: "/health/check",
        component: () => import("./components/lurgyhealthy/HealthyCheck.vue"),
        meta: { title: "居民体检", role: 2 },
      },
      {
        path: "/drugs",
        component: () => import("./components/drug/Drugs.vue"),
        meta: { title: "药品管理", role: 2 },
      },
      {
        path: "/caseHistory",
        component: () => import("./components/medicalrecord/CaseHistory.vue"),
        meta: { title: "病历管理", role: 2 },
      },
      {
        path: "/caseHistory/details",
        component: () =>
          import("./components/medicalrecord/CaseHistoryDetails.vue"),
        meta: { title: "病历详情", role: 2 },
      },
      {
        path: "/diseaseHealth",
        component: () => import("./components/dishealth/DiseaseHealth.vue"),
        meta: { title: "疾病健康", role: 2 },
      },
      {
        path: "/diseaseHealth/details",
        component: () => import("./components/dishealth/DiseaseDetails.vue"),
        meta: { title: "详情信息", role: 2 },
      },
      {
        path: "/visitinfo",
        component: () => import("./components/visitinfo/VisitInfo.vue"),
        meta: { title: "就诊信息", role: 2 },
      },
      {
        path: "/home/detail",
        component: () => import("./components/utils/noticeDetail.vue"),
        meta: { title: "公告信息", role: 2 },
      },
    ],
  },
  {
    path: "/dataAnalysis/drugsAnalysis",
    component: () => import("./components/statistic/DrugsAnalysis.vue"),
    meta: { title: "数据分析", role: 2 },
  },
  {
    path: "/forgotpassword",
    component: () => import("./components/utils/forgotPassword.vue"),
    meta: { title: "忘记密码" },
  },
  {
    path: "/403",
    component: () => import("./components/illegal/403.vue"),
    meta: { role: 200 },
  },
  {
    path: "/msg",
    component: () => import("./components/illegal/Msg.vue"),
    meta: { role: 200 },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes, // `routes: routes` 的缩写
});
router.beforeEach((to, from, next) => {
  //to 是到达的路由
  //from 是来源的路由信息
  //next 是帮助我们跳转路由的函数
  if (to.path === "/login") {
    localStorage.clear();
    next();
  } else {
    setTimeout(function () {
      let role = localStorage.getItem("role");
      if (to.meta.role == 200) {
        next();
      } else if (role != to.meta.role) {
        next("/403");
      } else {
        next();
      }
    }, 200);
  }
});
export default router;
